import request from "../../utils/request";

Page({
  data: {
    activesDetail: {},
    comments: [
      {
        id: 1,
        userIcon: '/images/avatar.png',
        userName: '用户1',
        content: '这个活动很有意义，学到了很多法律知识！'
      },
      {
        id: 2,
        userIcon: '/images/avatar.png',
        userName: '用户2',
        content: '慢跑活动很有趣，希望以后多举办！'
      }
    ],
    inputComment: ''
  },

  async onLoad(options) {
    console.log('接收参数:', options); 
    const id = options.id;
    //const detail = this.loadActivesDetail(id);
    let detail=await request.get("actives/"+id)
    this.setData({
      activesDetail: this.mapActiveDetail(detail),
    });

    let comments=await request.get("comments")
    //过滤评论
    comments=comments.filter(item => item.activesId == id);

    comments=comments.map(item => ({
      ...item,
      userIcon: '/images/avatar.png',
      userName: item.username,
      content: item.content
    }));
    this.setData({
      comments
    });
  },

   mapActiveDetail(data) {
  const genderMap = {
    'M': '男',
    'F': '女',
    'U': '未知'
  };

  return {
    id: data.id,
    cover: data.cover,
    title: data.title,
    ac_description: data.ac_description,
    userIcon: data.userInfo?.avatar || '/images/default-avatar.png',
    userName: data.userInfo?.username || '未知用户',
    description: data.description,
    contactInfo: {
      avatar: data.userInfo?.avatar || '/images/default-avatar.png',
      name: data.userInfo?.username || '',
      gender: genderMap[data.userInfo?.gender] || '未知',
      age: data.userInfo?.age || '',
      tel: data.userInfo?.tel || '',
      loc: data.userInfo?.location || ''
    }
  };
},


loadActivesDetail(id) {
    const mockData = {
      '1': {
        id: 1,
        cover: '/images/actives1.png',
        title: '法律宣讲 熟知诈骗手段',
        ac_description: '法律知识宣讲活动相关说明',
        userIcon: '/images/avatar.png',
        userName: '布布镜子',
        description: `1. 法律知识宣讲活动
                  2. 专业律师现场解答
                  3. 诈骗案例分析讲解
                  4. 参与可获普法手册`,
        contactInfo: {
          avatar: '/images/avatar.png',
          name: '布布镜子',
          gender: '女',
          age: 25,
          tel: '17313769125',
          loc: '祥和小区11栋3单元605室'
        }
      },
      '2': {
        id: 2,
        cover: '/images/actives2.png',
        title: '感知春色 小区绕城健身慢跑',
        ac_description: '春季健身慢跑活动相关说明',
        userIcon: '/images/avatar.png',
        userName: '彼酷哩',
        description: `1. 春季健身慢跑活动
                  2. 全程5公里健康跑
                  3. 专业教练带队
                  4. 参与赠送运动礼包`,
        contactInfo: {
          avatar: '/images/avatar.png',
          name: '彼酷哩',
          gender: '男',
          age: 30,
          tel: '17213769125',
          loc: '祥和小区1栋2单元205室'
        }
      },
      '3': {
        id: 3,
        cover: '/images/actives3.png',
        title: '小区八旬独身老人慰问',
        ac_description: '关爱独居老人活动相关说明',
        userIcon: '/images/avatar.png',
        userName: '哈哈哈',
        description: `1. 关爱独居老人活动
                  2. 生活物资捐赠
                  3. 陪伴聊天服务
                  4. 健康检查服务`,
        contactInfo: {
          avatar: '/images/avatar.png',
          name: '哈哈哈',
          gender: '女',
          age: 35,
          tel: '1581267890',
          loc: '祥和小区2栋4单元801室'
        }
      },
      '4': {
        id: 4,
        cover: '/images/actives4.jpg',
        title: '团圆佳节 中秋灯谜会',
        ac_description: '中秋主题灯谜会相关说明',
        userIcon: '/images/avatar.png',
        userName: '酷哩',
        description: `1. 中秋主题灯谜会
                  2. 传统灯笼制作
                  3. 月饼品鉴活动
                  4. 现场抽奖环节`,
        contactInfo: {
          avatar: '/images/avatar.png',
          name: '酷哩',
          gender: '男',
          age: 22,
          tel: '13118133365',
          loc: '祥和小区7栋1单元102室'
        }
      }
    };
    return mockData[id] || {};
  },

  navigateToContact() {
    const contactInfo = this.data.activesDetail.contactInfo;
    if (contactInfo) {
      wx.navigateTo({
        url: `/pages/contact-detail/contact-detail?contactInfo=${encodeURIComponent(JSON.stringify(contactInfo))}`
      });
    }
  },

  handleParticipate() {
    // 显示参与成功弹窗
    wx.showToast({
      title: '参与成功！',
      duration: 2000, // 弹窗显示时间（单位：毫秒）
      icon: 'none', // 不显示图标，默认显示对勾图标
      mask: true // 是否显示透明蒙层，防止触摸穿透
    });

    // 可选：如果需要在弹窗消失后执行其他操作
    setTimeout(() => {
      // 例如跳转到其他页面
      // wx.navigateTo({ url: '/pages/participate-success/participate-success' });
    }, 2000);
  },

  handleCommentInput(e) {
    this.setData({
      inputComment: e.detail.value
    });
  },

  async handleCommentSubmit() {
    const { inputComment } = this.data;
    if (inputComment.trim() === '') {
      wx.showToast({
        title: '评论内容不能为空',
        icon: 'none',
        duration: 1500
      });
      return;
    }

    const newComment = {
      id: Date.now(),
      userIcon: '/images/avatar.png',
      userName: '当前用户',
      content: inputComment
    };
   //POST http://localhost:8000/community/comments/
    let userId=getApp().globalData.userInfo?.userId||1;
    //发表评论后重新获取pinlun
   await request.post("comments/",{
      activesId:this.data.activesDetail.id,
      userId,
      content:newComment.content
    })
    .then(res=>{
      this.loadComments(this.data.activesDetail.id);
    })


    wx.showToast({
      title: '评论发表成功',
      icon: 'success',
      duration: 1500
    });
  },
  loadComments(id) {

    request.get("comments",{
      activesId:id
    })
    .then(res=>{
      res=res.filter(item => item.activesId == id);

      res=res.map(item => ({
        ...item,
        userIcon: '/images/avatar.png',
        userName: item.username,
        content: item.content
      }));

      this.setData({
        comments:res
      })
    })
  }
});